package com.shj.em.biz.sys.controller;

import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.hr.entity.Emp;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.biz.hr.service.EmpService;
import com.shj.em.util.DateUtils;
import org.springframework.stereotype.Controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sys.entity.EmpRank;
import com.shj.em.biz.sys.service.EmpRankService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 员工等级表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-08-08
 */
@Controller
@Slf4j
@RequestMapping("/sys/empRank")
public class EmpRankController extends BaseController<EmpRank>{

    private String prefix = "sys/emprank";

    @Autowired
    EmpRankService emprankService;

    @Autowired
    EmpService empService;

    @GetMapping("/gotoSearch")
    public String gotoSearch(Model model){
        model.addAttribute("flag", "empRank");
        return prefix + "/search";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<EmpRank> list(@RequestBody Map<String, Object> params){
        Page<EmpRank> page = emprankService.queryEmpRankPage(params);
        PageVO<EmpRank> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd/{empId}")
	String gotoAdd(@PathVariable("empId") Long empId, Model model){
        EmpRank empRank = new EmpRank();
        empRank.setEmpId(empId);
        empRank.setDefineDt(new Date());
        model.addAttribute("isEdit", false);
        model.addAttribute("emprank", empRank);
        addOtherInfo(model, empRank);
	    return prefix + "/add";
	}

	private void addOtherInfo(Model model, EmpRank empRank ){
        Emp emp = empService.selectById(empRank.getEmpId());
        empRank.setEmpName(emp.getEmpName());
        List<Cd> ranks = cdService.queryCdsByType(CdService.CD_TYPE_RANK);
        model.addAttribute("ranks", ranks);
    }

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        EmpRank emprank = emprankService.selectById(id);
		model.addAttribute("emprank", emprank);
        model.addAttribute("isEdit", true);
        addOtherInfo(model, emprank);
	    return prefix + "/add";
	}

	@Log("创建员工等级表")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(EmpRank emprank){
        try{
            //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2018);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            emprank.setDefineDt(cal.getTime());
            //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉

            emprankService.insert(emprank);
            return ResMsg.ok(new EmpRank());
        }catch(Exception e){
            log.error("创建员工等级表失败", e);
            return ResMsg.error("创建员工等级表失败");
        }

	}

    @Log("更新员工等级表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(EmpRank emprank){
        try{
            //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2018);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            emprank.setDefineDt(cal.getTime());
            //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉

            EmpRank db = emprankService.selectById(emprank.getId());
            int flag = DateUtils.compare(emprank.getDefineDt(), db.getDefineDt());
            if(flag == 0){
                emprankService.updateById(emprank);
            }else if(flag > 0){
                emprankService.deleteById(db.getId());
                emprankService.insert(emprank);
            }else{
                return ResMsg.error("日期不能早于上次");
            }

            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新员工等级表失败", e);
            return ResMsg.error("更新员工等级表失败");
        }

	}

	@Log("删除员工等级表")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            emprankService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除员工等级表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        emprankService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

